package capricornus.components.accordion
{
	import capricornus.components.scrollbar.ScrollPolicy;
	import capricornus.components.singleselectgroup.GroupEvent;
	import capricornus.components.singleselectgroup.SingleSelectGroup;
	import capricornus.components.tilelist.ComplexList;
	
	import flash.display.Sprite;

	[Event(name="treeItemClick", type="com.webgame.as3.components.tilelist.accordion.AccordionEvent")]
	/**
	 * Accordion类
	 * 示例：
	 * 		创建: var a:Accordion = new Accordion( 100, 100 );
	 * 		添加：需重载AccordionTitleList，如 AccordionTitleListEx
	 * 		a.beginAddList();
	 * 		a.addList( "aa", arrData0, AccordionTitleListEx );
	 * 		a.addList( "bb", arrData1, AccordionTitleListEx );
	 * 		....
	 * 		a.endAddList();
	 * 		监听事件：
	 * 		a.addEventListener( AccordionEvent.TREE_ITEM_CLICK, onTreeItemClick );
	 * 		function onTreeItemClick( e:AccordionEvent ):void { trace( e.selectItem ) }
	 * 
	 */	
	public class Accordion extends Sprite
	{
		private var _list:ComplexList;
		private var _w:int;
		private var _h:int;
		
		private var _groupItem:SingleSelectGroup;
		private var _groupList:SingleSelectGroup;
		private var _vecList:Vector.<AccordionTitleList>;
		
		private var _tileListheight:int;
		
		public function Accordion( w:int, h:int )
		{
			super();
			_w = w;
			_h = h;
			init();
		}
		
		private function init():void
		{
			_groupItem = new SingleSelectGroup();
			_groupList = new SingleSelectGroup();
			_vecList = new Vector.<AccordionTitleList>();
			
			_list = new ComplexList(1);
			_list.autoRefresh = false;
			_list.isDestroyOnRemove = true;
			_list.verticalScrollPolicy = ScrollPolicy.OFF;
			_list.verticalGap = 0;
			_list.setSize( _w, _h );
			addChild( _list );
			
			_groupItem.addEventListener( GroupEvent.SELECTED_CHANGE, onItemSelected, false, 0, true );
		}
		
		public function setLocation( x:int, y:int ):void
		{
			this.x = x;
			this.y = y;
		}
		
		protected function removeAll():void
		{
			_list.removeAllItem();
			_groupList.removeAllItem();
			_groupItem.removeAllItem();
			
			var h:int = 0;
			while( _vecList.length > 0 ) _vecList.pop();
		}
		
		public function set selectedListIndex( value:int ):void
		{
			if( value >= 0 && value < _vecList.length )
			{
				_groupList.selectedItem = _vecList[value];
			}
		}
		
		public function set selectedFixIndex( value100:int ):void
		{
			var value:int = value100/100 - 1;
			if( value >= 0 && value < _vecList.length )
			{
				_groupList.selectedItem = _vecList[value];
				(_vecList[value] as AccordionTitleList).subSelectedIndex = int(value100%100) - 1;
			}
		}
		
		public function beginAddList():void
		{
			removeAll();
		}
		
		public function addList( listName:String, arrData:Array, listClass:Class, extData:Object=null ):void
		{
			var tl:AccordionTitleList = new listClass( listName, _groupItem ) as AccordionTitleList;
			_tileListheight = tl.height;
			
			_list.addItem( tl );
			_groupList.addItem( tl );
			_vecList.push( tl );
			
			tl.setData( arrData, Boolean(extData) );
		}
		
		public function endAddList():void
		{
			for( var j:int=0; j<_vecList.length; j++ )
			{
				_vecList[j].height = _h - (_vecList.length-1) * _tileListheight;
			}
			_list.refresh();
		}
		
		private function onItemSelected( e:GroupEvent ):void
		{
			dispatchEvent( new AccordionEvent( AccordionEvent.TREE_ITEM_CLICK, _groupItem.selectedItem ) );
		}
	}
}






















